/**
 * @Description:
 * @author 云离
 * @date: 2020-02-05
 */
import axios from "../apis/api";

const initialState = {
  vwAgentCashStat: {},
  vwAgentCashDayStat: {},
  vwBusinessCashDayStat: {},
  vwBusinessCashStat: {},
  vwSalesmanCashStat: {},
  statData: {},
  vwSalesmanCashDayStat: [],
  menus: [],
  user: {},
  isAgent: false, // 是否是代理商
  isChannel: false, // 是否是通道商
  isSalesman: false, // 业务员
  isAdmin: false, // 业务员
  isGroup: false, // 组长
  payTypeList: [], // 支付类型
  groups: [], // 分组
  agentList: [], // 码商带来
  banks: [], // 银行
  address: [] //模拟地址
};

export const actionsTypes = {
  FETCH_SYSTEM_MAIN_DATA: "FETCH_SYSTEM_MAIN_DATA",
  FETCH_CHANNEL_SELECT_DATA: "FETCH_CHANNEL_SELECT_DATA"
};

export function reducer(state = initialState, action) {
  switch (action.type) {
    case actionsTypes.FETCH_SYSTEM_MAIN_DATA:
      let isAgent = false;
      let isChannel = false;
      let isSalesman = false;
      let isGroup = false;
      let isAdmin = false;
      const user = (action.result[0] && action.result[0].data.user) || {};
      if (user.roles) {
        user.roles.forEach(role => {
          if (role.roleKey === "agent") {
            isAgent = true;
          } else if (role.roleKey === "admin2" || role.roleKey === "admin") {
            isAdmin = true;
          } else if (role.roleKey === "channel") {
            isChannel = true;
          } else if (role.roleKey === "salesman") {
            isSalesman = true;
          } else if (role.roleKey === "group") {
            // 组长
            isGroup = true;
          }
        });
      }
      return {
        ...state,
        vwAgentCashStat:
          (action.result[1] && action.result[1].data.vwAgentCashStat) || {},
        vwAgentCashDayStat:
          (action.result[1] && action.result[1].data.vwAgentCashDayStat) || {},
        vwBusinessCashStat:
          (action.result[1] && action.result[1].data.vwBusinessCashStat) || {},
        vwBusinessCashDayStat:
          (action.result[1] && action.result[1].data.vwBusinessCashDayStat) ||
          {},
        vwSalesmanCashStat:
          (action.result[1] && action.result[1].data.vwSalesmanCashStat) || {},
        vwSalesmanCashDayStat:
          (action.result[1] && action.result[1].data.vwSalesmanCashDayStat) ||
          {},
        statData: (action.result[1] && action.result[1].data) || {},
        menus: (action.result[0] && action.result[0].data.menus) || [],
        user: user,
        isAgent,
        isSalesman,
        isAdmin,
        isChannel,
        isGroup
      };
    case actionsTypes.FETCH_CHANNEL_SELECT_DATA:
      return {
        ...state,
        payTypeList: (action.result[0] && action.result[0].data) || [], // 支付类型
        groups: (action.result[1] && action.result[1].data) || [], // 分组
        agentList: (action.result[2] && action.result[2].data) || [], // 码商带来
        banks: (action.result[3] && action.result[3].data) || [], // 银行
        address: (action.result[4] && action.result[4].data) || [] //模拟地址
      };
    default:
      return state;
  }
}

export const actions = {
  fetchInitData: function(next) {
    return async function(dispatch) {
      let result = [];
      try {
        result = await Promise.all([
          axios.get("/index/data"),
          axios.get("/system/main/data")
        ]);
      } catch (e) {}
      next && next();
      dispatch({
        type: actionsTypes.FETCH_SYSTEM_MAIN_DATA,
        result
      });
    };
  },
  fetchChannelSelectList: function() {
    return async function(dispatch) {
      let result = [];
      try {
        result = await Promise.all([
          axios.get("/channel/payerPayType/selfPayType"),
          axios.get("/channel/channelAccount/channelGroup"),
          axios.get("/channel/channelAccount/channelUser"),
          axios.get("/channel/channelAccount/banks"),
          axios.get("/channel/channelAccount/location")
        ]);
        console.log(result);
      } catch (e) {}
      dispatch({
        type: actionsTypes.FETCH_CHANNEL_SELECT_DATA,
        result
      });
    };
  }
};
